<template>
	<view class="padding" :style="'height:'+screenHeight+'px;'">
		<view class="boxconten" style="">
			<view class="one">
				<view class="" style="color: #F56C6C;" v-if="item.Status==1">
					<span>未支付</span>
				</view>
				<view class="" style="color: #C0C0C0;" v-if="item.Status==10">
					<span>已取消</span>
				</view>
				<view class="" style="color: #27BF86;" v-if="item.Status==2">
					<span>待就诊</span>
				</view>
				<view class="" style="color: #0197F6;" v-if="item.Status==20">
					<span>已完成</span>
				</view>
			</view>
			<view class="two">
				<view class="onea" style="twoicon"><u-icon name="clock" color="#878787" size="40" :label="item.VisitDay+' '+item.VisitTime"></u-icon></view>
				<view class="onea" style="twoicon"><u-icon name="more-circle" color="#a1a1a1" size="40" :label="'科室： '+item.DptName"></u-icon></view>
				<view class="onea" style="twoicon"><u-icon name="account" color="#a1a1a1" size="40" :label="'医师： '+item.DoctorName"></u-icon></view>
				<view class="onea" style="twoicon"><u-icon name="account" color="#a1a1a1" size="40" :label="'就诊人： '+item.PatientName"></u-icon></view>
				<view class="onea" style="twoicon"><u-icon name="account" color="#a1a1a1" size="40" :label="'金额： '+item.GHF"></u-icon></view>
				<view class="onea" style="twoicon"><u-icon name="chat" color="#a1a1a1" size="40" :label="'情况备注：\n'+item.Memo"></u-icon></view>
			</view>
			<view class="three" v-if="item.Status==2">
				<span>预约单号：{{item.OrderNumber}}</span>
			</view>
			<view class="qrcodes" v-if="item.Status==2">
				<uv-qrcode ref="qrcode" size="300rpx" :value="item.OrderNumber"></uv-qrcode>
			</view>
			<u-radio-group v-model="value" style="width: 100%;padding-top: 30rpx;">
				
				<view class="disbetween"  @click="radioChange('2')" style="width: 100%;padding: 20rpx 0;">
					<view class="dis">
						<u-icon name="/static/icon/wxzf.png" size="70"></u-icon>
						<span>&emsp;微信支付</span>
					</view>
					<u-radio class="zfxz" name="2"></u-radio>
				</view>
			</u-radio-group>
			<view class="four">
				<view v-if="item.Status==1">
					<u-button @click="payment(item.OrderNumber)" v-if="item.GHF!=0" type="success">支付</u-button>
				</view>
				<view class="" style="background-color: #C0C0C0;" v-if="item.Status==10">
					<u-button @click="deleteshow=true" type="default">删除记录</u-button>
				</view>
				<view v-if="item.Status==1" style="padding-top: 20rpx;">
					<u-button @click="cancellationshow=true">取消预约</u-button>
				</view>
				<view v-if="item.Status==2" style="padding-top: 20rpx;">
					<u-button @click="cancellationshow2=true">申请退款</u-button>
				</view>
				<view v-if="item.Status==20">
					<u-button type="primary">已完成</u-button>
				</view>
			</view>
		</view>
		<u-modal v-model="cancellationshow" :content="cancellationtext" @confirm="cancellation" :mask-close-able="true" :show-cancel-button="true"></u-modal>
		<u-modal v-model="deleteshow" :content="deletetext" @confirm="deletes" :mask-close-able="true" :show-cancel-button="true"></u-modal>
		<u-modal v-model="cancellationshow2" :content="cancellationtext2" @confirm="refund" :mask-close-able="true" :show-cancel-button="true"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
						name: '1',
						text: '医保支付',
						icon: ''
					},
					{
						name: '2',
						text: '微信支付',
						icon: ''
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '2',
				screenHeight: getApp().globalData.screenHeight-44,
				OrderId:'',
				cancellationtext:'取消预约后将无法撤回，是否取消',
				cancellationtext2:'申请退款后将无法撤回，是否退款',
				cancellationshow:false,
				cancellationshow2:false,
				deletetext:'删除记录后无法找回，是否删除',
				deleteshow:false,
				item: {
					DoctorName: "",
					DptId: "",
					DptName: "",
					Memo: "",
					Mobile: "",
					OrderId: "",
					OrderNumber: "",
					PatientName: "",
					PayNumber: "",
					Status: '',
					VisitDay: "",
					VisitTime: "",
					GHF:0
				},
				openid:'',
				sessionkey:'',
				appid:''
			}
		},
		onLoad(option) {
			this.OrderId=option.OrderId
			console.log("option.OrderId",option.OrderId)

			this.getDetail()
			
			
		},
		methods: {
			radioChange(e) {
				console.log(e);
				this.value=e
			},
			getDetail(){
				uni.showLoading({
					title: '请稍后'
				})
				this.$request("Order","Detail",{
					OrderId:this.OrderId
				}).then(res=>{
					console.log("详情",res)
					if (res.code == 1) {
						this.item=res.data
						uni.hideLoading();
						
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
					console.log("11111",this.item)
				})
			},
			deletes(){
				this.$request("Order", "Delete", {
					OrderNo: this.item.OrderNumber
				}).then(res => {
					if (res.code == 1) {
						console.log("res",res)
						if (res.code == 1) {
							uni.showToast({
								title: '删除成功',
								icon: 'none',
								duration: 1000
							});
							setTimeout(function() {
								uni.redirectTo({
									url: `/pages/register/Scheduling/record/record?current=${3}`,
									fail(error) {
										console.log(error)
									}
								})
								return
							}, 1000);
						}
					}else{
						uni.showToast({
							title: '删除失败',
							icon: 'none',
							duration: 1000
						});
					}
				})
			},
			pays(){
				console.log("OrderNumber",this.OrderNumber)
				this.$request("pay","Hispay",{
					OrderNo:this.item.OrderNumber,
					openid:this.item.openid,
				}).then(resto=>{
					console.log(resto)
					if(resto.code==1){
						uni.showLoading({
							title: '请等待'
						})
						uni.requestPayment({
							provider:'wxpay',
							timeStamp:resto.data.Timestamp,
							package:resto.data.Package,
							nonceStr:resto.data.Noncestr,
							signType:resto.data.SignType,
							paySign:resto.data.paySign,
							success:(resshree)=>{
								console.log("resshree",resshree)
								uni.showToast({
									title: '支付成功',
									icon: 'none',
									duration: 1000
								});
								setTimeout(function() {
									uni.navigateBack()
									return
								}, 1000);
							},
							fail:(err)=>{
								// console.log("err",err)
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									duration: 1000
								});
								setTimeout(function() {
									uni.navigateBack()
									return
								}, 1000);
							}
							
						})
																	
					}
					
				})
				
			},
			payment(OrderNumber){
				// uni.redirectTo({
				// 	url: `/pages/payment/ddzf?OrderNo=${OrderNumber}`,
				// 	fail(error) {
				// 		console.log(error)
				// 	}
				// })
				// return
				this.$request("pay","Hispay",{
						OrderNo:OrderNumber,
					}).then(resa=>{
						console.log("订单号",resa)
						WeixinJSBridge.invoke('getBrandWCPayRequest', {
								"appId": resa.data.appId, //公众号ID，由商户传入     
								"timeStamp": resa.data.timeStamp, //时间戳，自1970年以来的秒数     
								"nonceStr": resa.data.nonceStr, //随机串     
								"package": resa.data.package,
								"signType": resa.data.signType, //微信签名方式：     
								"paySign": resa.data.paySign //微信签名 
							},
							function(res) {
								// then.sss=res
								// console.log("支付",res)
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									uni.showToast({
										title: "成功支付",
										icon: "none",
										duration: 1000,
									});
									setTimeout(function() {
										uni.redirectTo({
											url: `/pages/register/Scheduling/record/record?current=${3}`,
											fail(error) {
												console.log(error)
											}
										})
										return
									}, 1000);
									
								}else{
									uni.showToast({
										title: "支付未成功",
										icon: "none",
										duration: 1000,
									});
									setTimeout(function() {
										uni.redirectTo({
											url: `/pages/register/Scheduling/record/record?current=${2}`,
											fail(error) {
												console.log(error)
											}
										})
										return
									}, 1000);
								}
							});
					})
				// uni.login({
				//     success: async resCode => {
				// 		console.log("rescode",resCode)
				//         let res = await this.$request('auth', 'getopenid', {
				//             Code: resCode.code
				//         });
						
				//         if (res.code === 1) {
				// 			console.log("res",res)
				//             this.appid = res.data.appid;
				//             this.sessionkey = res.data.session_key;
				//             this.openid = res.data.openid;
				// 			if(GHF==0){
				// 				return
				// 			}else{
				// 				this.pays()
				// 			}
							
				//         }
				//     }
				// })
			},
			cancellation(){
				console.log("进了")
				this.$request("Order", "Cancel", {
					OrderNo: this.item.OrderNumber
				}).then(res => {
					console.log("res",res)
					if (res.code == 1) {
						uni.showToast({
							title: '取消成功',
							icon: 'none',
							duration: 1000
						});
						setTimeout(function() {
							uni.redirectTo({
								url: `/pages/register/Scheduling/record/record?current=${1}`,
								fail(error) {
									console.log(error)
								}
							})
							return
						}, 1000);
					}else{
						uni.showToast({
							title: '取消失败',
							icon: 'none',
							duration: 1000
						});
					}
				})
			},
			refund(){
				console.log("进了")
				this.$request("pay", "refund", {
					OrderNo: this.item.OrderNumber,
					RefundReson:''
				}).then(res => {
					console.log("res",res)
					if (res.code == 1) {
						uni.showToast({
							title: '退款成功',
							icon: 'none',
							duration: 1000
						});
						setTimeout(function() {
							uni.redirectTo({
								url: `/pages/register/Scheduling/record/record?current=${3}`,
								fail(error) {
									console.log(error)
								}
							})
							return
						}, 1000);
					}else{
						uni.showToast({
							title: '退款失败',
							icon: 'none',
							duration: 1000
						});
					}
				})
			},
			formattedDatemonth(VisitDay) {
				const date = new Date(VisitDay);
				return date.toLocaleDateString('zh-CN', {
					month: 'short'
				}).replace(/\//g, '月');
			},
			formattedDateday(VisitDay) {
				const date = new Date(VisitDay);
				return date.toLocaleDateString('zh-CN', {
					day: 'numeric'
				}).replace(/\//g, '月');
			}
		}
	}
</script>

<style>
	@import './record.css'
</style>